package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.ReplyDao;
import com.qf.lanzhou.blog.api.pojo.entity.Reply;
import com.qf.lanzhou.blog.api.pojo.vo.ReplyVo;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 是柠檬甜
 * @ClassName ReplyDaoImpl
 * @description 回复数据库接口实现类
 * @date 2020/12/18 15:34
 * @modifier 是柠檬甜
 * @date 2020-12-18 15:34
 * @Version V1.0
 */
public class ReplyDaoImpl implements ReplyDao {
    private static QueryRunner queryRunner;
    static {
        queryRunner=new QueryRunner(DruidUtil.getDataSource());
    }
    @Override
    public List select(Long commentId, Long articleId) {
        Object[] obj={commentId,articleId};
        String sql="SELECT * FROM t_reply WHERE comment_id=? AND article_id=?;";
        BeanListHandler<List> replyList = BeanProcessor.getBeanListHandler(ReplyVo.class);
        List list=null;
        try {
            list= queryRunner.query(sql, replyList, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Boolean insert(Long commentId, Long articleId, String replyContent,String replyUserName) {
        Object[] obj={commentId,articleId,replyContent,replyUserName,commentId};
        String sql="INSERT INTO t_reply(comment_id,article_id,reply_content,reply_user_name,comment_user_name) \n" +
                "VALUES (?,?,?,?,(SELECT user_name  comment_user_name FROM t_comment WHERE comment_id=?));";
        int update=0;
        try {
            update = queryRunner.update(sql, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (update==0){
            return false;
        }
        return true;
    }
}
